//理赔相关

const { claimsModel } = require('./models/claimsModels')



module.exports.getClaims = async ({_id,currentPage,pageSize}) =>{
    console.log({_id,currentPage,pageSize});
    if (_id){
        //数据条数
        const total = (await claimsModel.find({_id,isDel:false}).populate({
            path:'ordersId',
            populate:{
                path:'customersId insuresID',
            },

        })).length;

        const data = await claimsModel.find({_id,isDel:false}).populate({
            path:'ordersId',
            populate:{
                path:'customersId insuresID',
            },
        }).limit(pageSize - 0).skip(currentPage*pageSize);
        
        const pageTotal = Math.floor(total / pageSize);

        return {data,total,pageTotal}
    }else{
        const total = (await claimsModel.find({isDel:false}).populate({
            path:'ordersId',
            populate:{
                path:'customersId insuresID',
            },
        })).length;

        const data =await claimsModel.find({isDel:false}).populate({
            path:'ordersId',
            populate:{
                path:'customersId insuresID',
            },
        }).limit(pageSize - 0).skip(currentPage*pageSize)

        const pageTotal = Math.floor(total / pageSize);

        return {data,total,pageTotal}
    }
}

//添加理赔
/**
 * 
 * @param 传对象{applyTime:String,handleTime:String,images:Array,accidentTime:String,ordersId:ObjectId}
 */
module.exports.addClaims = async param => {
    return await claimsModel.create(param)
}

//修改理赔信息
module.exports.modifyClaims = async ({ _id, update }) => {
    return await claimsModel.findByIdAndUpdate(_id, update, { new: true })
}
module.exports.handleClaimById = async (param) => {
    return await claimsModel.find(param)
}

//删除理赔

module.exports.delClaims = async (_id) => {
    const reslut = claimsModel.findById(_id)
    //申请中的理赔不能删除
    if (reslut.state != 1) {
        return await claimsModel.findByIdAndUpdate(_id, { isDel: true }, { new: true })
    }

}

